package logic

import (
	"context"
	"errors"
	"srv/models/mysqlinit"
	"time"

	"srv/internal/svc"
	"srv/srv"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseInfoLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseInfoLogic {
	return &HouseInfoLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseInfoLogic) HouseInfo(in *srv.HouseInfoRequest) (*srv.HouseInfoStatusResponse, error) {
	// todo: add your logic here and delete this line
	h := mysqlinit.House{}
	err := h.FindHouse(in.HouseId)
	if err != nil {
		logx.Error("获取房源详情失败")
		return nil, errors.New("获取房源详情失败")
	}
	var hc []mysqlinit.HouseCover
	mysqlinit.DB.Debug().Where("house_id = ?", in.HouseId).Find(&hc)
	var list []*srv.HouseCover
	for _, m := range hc {
		listHouse := srv.HouseCover{
			HouseId: int64(m.HouseId),
			Living:  m.Living,
			Sleep:   m.Sleep,
			Kitchen: m.Kitchen,
			Toilet:  m.Toilet,
		}
		list = append(list, &listHouse)
	}
	return &srv.HouseInfoStatusResponse{
		UserId:      int64(h.UserId),
		City:        h.City,
		Address:     h.Address,
		HouseCode:   h.HouseCode,
		HouseFloor:  h.HouseFloor,
		HouseAcre:   h.HouseAcre,
		HousePrice:  float32(h.HousePrice),
		Describe:    h.Describe,
		CreatedAt:   h.CreatedAt.Format(time.DateTime),
		HouseStatus: int64(h.HouseStatus),
		List:        list,
	}, nil
}
